import { BlockVolume } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { GameComponent } from "@sapi-game/main";
import { Elevator } from "./elevator";
import { eleMainState } from "../state";

/**电梯门控 */
export class ElevatorDoorComponent extends GameComponent<eleMainState> {
    override onAttach(): void {}

    /**初始化 */
    load() {
        this.killDoorEntity();
        this.state.eleList.forEach((t) => {
            //遍历每层
            for (let i = 1; i <= t.config.maxFloor; i++) {
                const dim = Dimensions.Overworld;
                const loc = t.baseLoc;
                const floorY = this.context.utils.calcFloorY(i);
                const doorA = dim.spawnEntity("partygames:elevatordoor", {
                    x: loc.x + 2,
                    y: floorY,
                    z: loc.z - 0.5,
                });
                const doorB = dim.spawnEntity("partygames:elevatordoor", {
                    x: loc.x + 2,
                    y: floorY,
                    z: loc.z - 1.5,
                });
                doorA.teleport(doorA.location, { rotation: { x: 0, y: -90 } });
                doorB.teleport(doorB.location, { rotation: { x: 0, y: 90 } });
                //重置铁门
                this.setDoorBlock(t, floorY, false);
            }
        });
    }

    OpenOrCloseDoor(elevator: Elevator, isOpen: boolean) {
        const floorY = this.context.utils.calcFloorY(elevator.getState().floor);
        const baseLoc = elevator.baseLoc;
        this.runner.run(async (r) => {
            elevator.doorAnimating = true;
            const doors = Dimensions.Overworld.getEntities({
                location: { x: baseLoc.x + 1, y: floorY, z: baseLoc.z - 1 },
                maxDistance: 3,
                type: "partygames:elevatordoor",
            });
            //开/关门动画
            doors.forEach((d) =>
                d.playAnimation(
                    isOpen ? "animation.elevatordoor.open" : "animation.elevatordoor.close",
                    isOpen ? { blendOutTime: 10000 } : undefined
                )
            );
            if (!isOpen) {
                await r.wait(20);
            }
            //填充方块
            r.do(() => this.setDoorBlock(elevator, floorY, isOpen));
            if (isOpen) await r.wait(20);
            r.do(() => {
                elevator.doorAnimating = false;
            });
        });
    }

    /**设置指定楼层高度的铁门开关状态 */
    private setDoorBlock(elevator: Elevator, floorY: number, open: boolean) {
        const baseLoc = elevator.baseLoc;
        const volume = new BlockVolume(
            { x: baseLoc.x + 1, y: floorY, z: baseLoc.z - 1 },
            { x: baseLoc.x + 1, y: floorY + 2, z: baseLoc.z - 2 }
        );
        Dimensions.Overworld.fillBlocks(volume, open ? "minecraft:air" : "minecraft:iron_block");
    }

    private killDoorEntity() {
        Dimensions.Overworld.getEntities({
            location: this.state.eleList[0].baseLoc,
            maxDistance: 100,
            type: "partygames:elevatordoor",
        }).forEach((e) => e.remove());
    }

    override onDetach(): void {
        this.killDoorEntity();
    }
}
